<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-09
 * Time: 14:34
 */

namespace app\service;


use app\model\CardsModel;
use app\model\CardVisitedModel;
use think\Exception;

/**
 * 名片访问
 * Class CardVisitedService
 * @package app\service
 */
class CardVisitedService extends BaseService
{
    public $cardModel;
    public $cardVisitedModel;
    public function __construct()
    {
        $this->cardModel=new CardsModel();
        $this->cardVisitedModel = new CardVisitedModel();
    }



    /**
     * 添加访问者
     * @param $add
     * @return bool|false|int
     */
    public function addVisited($add){
        try{
           return $this->cardVisitedModel->insertGetId($add);
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 获取访问者列表
     * @param $where
     * @param string $field
     * @param int $limit
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function queryLimitList($where,$field='b.headimgurl',$order='visited_time desc',$limit=8)
    {
        try{
            $data = $this->cardVisitedModel
                ->alias('a')
                ->join('zkhb_user b','a.visited_id=b.id','left')
                ->where($where)
                ->limit($limit)
                ->order($order)
                ->column($field);
            return $data;
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 访问者信息
     * @param $where
     * @param string $field
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function findVisited($where,$field='*')
    {
        try{
            $data = $this->cardVisitedModel
                ->where($where)
                ->field($field)
                ->find();
            if($data)
                $data->toArray();
            return $data;
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }

    /**
     * 计数
     * @param $add
     * @return bool|int|string
     */
    public function queryCount($where){
        try{
           return $this->cardVisitedModel->where($where)->count();
        }catch (Exception $ex){
            LogService::S()->writeLog('Exception',$ex->getMessage());
            return false;
        }
    }



}